package com.ly.mpayment.persist.service.timer;

import com.ly.mpayment.persist.entity.ProductPriceUp;
import com.ly.mpayment.persist.entity.ProductTimerUp;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
import com.ly.mpayment.persist.service.downshopproduct.IDownShopProductService;
import com.ly.mpayment.persist.service.upshoppro.IUpShopProService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service
public class ProductTimerUpServiceImpl extends AbstractService<ProductTimerUp, Serializable> implements ProductTimerUpService{


    @Autowired
    private ProductPriceUpService productPriceService;

    @Autowired
    private IDownShopProductService shopProductService;

    @Autowired
    private IUpShopProService updateProductPriceUp;

    @Override
    public void saveProduct(ProductTimerUp timer) {

        this.saveOrUpdate(timer);

    }

    @Override
    public boolean updateProductTimer(JSONObject json) {
        ProductTimerUp timer = this.findByUnique("id",json.getString("id"));
        if(timer != null){
            timer.setEnabled(json.getBoolean("enabled"));
            this.updateEntity(timer);
            return true;
        }
        return false;
    }

    @Override
    public Page<ProductTimerUp> queryProductTimerPage(Map<String, Object> parameters, int start, int pageSize) {
        if(parameters.containsKey(":upName")){
            parameters.put(":upName","%"+parameters.get("upName")+"%");
        }
        //private String downName;
        String sql = "select b from ProductTimerUp b where 1 = 1 #[ and  b.operator = :operator ]#  #[ and  b.province = :province ]# " +
                " #[ and  b.enabled = :enabled ]#  #[ and  b.status = :status ]#   " +
                " #[ and  b.busname = :busname ]#   #[ and  b.createUser = :createUser ]#  " +
                " #[ and  b.createTime like :downName ]#  " ;

        if(parameters.containsKey(":starttime")){
            sql += " and  b.createTime >=  '"+parameters.get(":starttime")+"'";
            parameters.remove(":starttime");
        }
        if(parameters.containsKey(":endtime")){
            sql += " and  and  b.createTime <=  '"+parameters.get(":endtime")+"'";
            parameters.remove(":endtime");
        }
        sql += "  order by b.createTime desc ";
        return this.findPageByNamed(sql , parameters, start, pageSize);
    }


    @Override
    public List<ProductPriceUp> detailProductTimer(String id) {
        return productPriceService.detailProductTimerAll(id);
    }

    @Override
    public List<ProductTimerUp> findExecute(String nowDateYMDHMS) {
        String sql = "select b from ProductTimerUp b where  b.enabled = 0 and b.status = 0 and DATE_FORMAT(b.createTime,'%Y-%m-%d') = ? " +
                " order by b.executeTime " ;
        return this.find(sql,nowDateYMDHMS);
    }

    @Override
    public void updateProduct(ProductTimerUp timer) {
        this.updateEntity(timer);
    }

    @Override
    public boolean executeProductTimer(String id) {
        ProductTimerUp timer = this.findUnique(" select b from ProductTimerUp b where  b.id = ? and b.enabled = 0 and b.status = 0 ",id);
        if(timer == null){
            return false;
        }
        List<ProductPriceUp> productPrices = detailProductTimer(timer.getId());
        if(!CollectionUtils.isEmpty(productPrices)){
            for (ProductPriceUp price : productPrices ) {
                //修改产品价格
                updateProductPriceUp.updateProductPriceUp(price);
            }
        }
        timer.setStatus(1);
        updateProduct(timer);

        return true;
    }
}
